/*=================================================
    File:	zcom_uiIMMain.c

	Module:	ui
	Author:	Mike	

=================================================*/

#include <zcom_uiIMMain.h>
#include <ui.h>

#include <poller.h>
#include <mainthread.h>
#include <display.h>
#include <IMManager.h>

#if 1
#define dbgf	debugf
#endif

//We use voice-mail icon here for IM temporarily
//When better icon available, replace it
//by Mike
#define _ICON_IM_MAIL	DISP_ICON_VOICE_MAIL


typedef struct
{
	bool bInit;
	struct pollerTask checkTask;
	IM_INBOX _in_last;
	int  notifyNr;
} _uiIMMain;

static _uiIMMain	_imm = {};

//for Inbox full issue
//save the last one to keep it in new state... 

static void IMCheckTask(struct pollerTask *TaskP)
{
	//check for inbox full issue
	//by Mike
	int used=0, total;
	if(IM_InBox_GetSize(&used, &total))
	{
		//valid
		if(total>0)
		{
			if(used==total)
			{
				if(strlen(_imm._in_last.number)==0)
				{
					//save
					if(IM_InBox_GetMsgByIndex(used-1, &_imm._in_last))
					{
						_imm.notifyNr = 0;
						
					}
					else
					{
						dbgf("[UI IMMain] Fail to save!\n");
					}
				}
				
				//Always delete the last one, since it has been full! 
				//Maybe better always do update?
				//Need check if it will show the next one(after last) to user?...
				//by Mike
				IM_InBox_DeleteMsgByIndex(used-1);

			}
			else
			{
				if(used<total-1 && strlen(_imm._in_last.number))
				{
					//add it to inbox, delay receiving, haha...
					IM_InBox_AddMsg(_imm._in_last.number, _imm._in_last.msg);
					memset(&_imm._in_last, 0, sizeof(IM_INBOX));
				}

			}

			//call inbox update here
			//by Mike
			zcom_uiIMInbox_Update(used==total?used-1:used); //Exclude the last one

			//for new flag
			if(uiMenuLevelGet()>0)
			{
				//clear
				zcom_uiIMInbox_ClearNewMsgFlag();
			}
		}
		
	}

	if(zcom_uiIMInboxGetUnreadMail() || strlen(_imm._in_last.number))
	{
		//set icon
		dispIconSet(uiDisplay(), _ICON_IM_MAIL, 1);
	}
	else
	{
		//reset icon
		dispIconSet(uiDisplay(), _ICON_IM_MAIL, 0);
	}
	
	//for notification issue
	if(used>=total-1)
	{
		if(_imm.notifyNr==0)
		{
			//
			char str[32] = {};
			spr(str, sizeof(str), langGet(LANG_Menu_IM_Full_Format), langGet(LANG_Menu_IM_INBOX));
			uiMenuDialogMesgTimed(
				LANG_Menu_IM_INBOX,
				str 
				);
			_imm.notifyNr++;
		}
		else
		{
			_imm.notifyNr++;
			if(_imm.notifyNr>=9) //~30 s
			{
				_imm.notifyNr = 0;
			}
		}
	}

	//next check
	pollerTaskLongTimeoutRelSeconds(
				&_imm.checkTask, 3);
}

void zcom_uiIMMainInit()
{
	if(_imm.bInit)
		return;
    _imm.bInit = true;

	mainthreadTaskInit(
        &_imm.checkTask, "IM Main Checker", IMCheckTask );

	//schedule the checker
	pollerTaskLongTimeoutRelSeconds(
				&_imm.checkTask, 5);

}

static const UIMenuNode MenuIMMainSelectNodes[] = {
    {
        .text        = {LANG_Menu_IM_WRITE}, //write
        .selectCB    = zcom_uiIMWriteStart,
        .selectCBarg = NULL
    },

	{
        .text        = {LANG_Menu_IM_INBOX}, //inbox
        .selectCB    = zcom_uiIMInboxStart,
        .selectCBarg = NULL
    },

	{
        .text        = {LANG_Menu_IM_DRAFT}, //draft
        .selectCB    = zcom_uiIMDraftStart,
        .selectCBarg = NULL
    },

	{
        .text        = {LANG_Menu_IM_CLEAR}, //clear
        .selectCB    = zcom_uiIMClearStart,
        .selectCBarg = NULL
    },

	{
        .text        = {LANG_Menu_IM_MEMORY}, //memory
        .selectCB    = zcom_uiIMMemoryStart,
        .selectCBarg = NULL
    },

};


void zcom_uiIMMainStart(void * arg)
{
	UIMenuListInit menu = {};

    menu.title.textIdx = LANG_Menu_IM_title;
    menu.nNodes = sizeof(MenuIMMainSelectNodes) / sizeof(MenuIMMainSelectNodes[0]);
    menu.currNode = 0;
    menu.nDispLines = 0;
    menu.nodes = MenuIMMainSelectNodes;

	uiMenuListInit(&menu);
}

//for new message issue
//used by Main UI
bool  zcom_uiIMInbox_GetNewMsgFlag()
{
	bool bNew = IM_InBox_GetNewMsgFlag();
	if(bNew)
	{
		//only show on main UI
		//by Mike
		if(uiMenuLevelGet()==0)
		{
			return true;
		}
		//clear
		zcom_uiIMInbox_ClearNewMsgFlag();
	}
	return false;
}

void  zcom_uiIMInbox_ClearNewMsgFlag()
{
	IM_InBox_ClearNewMsgFlag();
}
